using UnityEngine;
using System.Collections;

public class BallDrag : MonoBehaviour 
{
	float speed = 5.0F;
	float maxSpeed = 15.0F;
	Transform dragObj = null;
	RaycastHit hit;
	float length;
	public Camera cam;

	void FixedUpdate()
	{
	
		if ( Input.GetMouseButton(0) )
		{  
			Ray ray = cam.ScreenPointToRay(Input.mousePosition);
				
			if (!dragObj )
			{   
				if (Physics.Raycast(ray, out hit) && hit.rigidbody)
				{
					if ( hit.collider.tag == "Ball" )
					{
						dragObj = hit.transform; 
						length = hit.distance; 
					}
				}
			} 
			else 
			{  
				Vector3 vel = (ray.GetPoint(length) - dragObj.position) * speed; 
				if (vel.magnitude > maxSpeed) vel *= maxSpeed / vel.magnitude;
				dragObj.rigidbody.velocity = vel;
			}	
		}
		else 
		{  
			dragObj = null; 
		}
	}
	
	void OnCollisionEnter ( Collision hit )
	{
		if ( hit.gameObject.tag == "FinishLine" )
		{
			OldManManager.Instance.StartMoving();
			QuestionList.Instance.GenerateRandomQuiz();
		}
	}
}
